﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
using UnityEngine.Events;

public class BubbleBlending : MonoBehaviour
{
    // Start is called before the first frame update
    public Vector3 centerPos2;
    public GameObject Bubble2Level;
    private GameObject bubble2Level;
    private bool isBigger = false;
    public GameObject colliders;
    public GameObject Player;
    public bool IsColliding = false;
    public bool bubble2HasDestroyed = false;
    public Material DarkMaterial;
    public Material LightMaterial;


    //相机震动
    private Vector3 deltaPos=Vector3.zero;
    public GameObject mainCamera;
    //上述为useless
    public float ShakeDuration = 0.3f;
    public float ShakeAmplitude = 1.2f;
    public float ShakeFrequency = 2.0f;
    private float ShakeElapedTime = 0f;


    //ColorChnage
    public float checkTime=1f;
    public float deltaColorTime = 0.1f;
    public float tempTime;
    public Material BlendMaterial4;
    public Material BlendMaterial3;
    public Material BlendMaterial2;
    public Material BlendMaterial1;
    public bool sitFlag = false;

    public GameObject WoodBench;
    public bool isSitting = false;


    public enum BubbleState
    {
        Light,
        Dark
    }
    public BubbleState thisState ;
    void Start()
    {
        thisState = BubbleState.Dark;
        mainCamera = GameObject.FindWithTag("MainCamera");
        WoodBench = GameObject.FindWithTag("WoodBench");
        //if (virtualCamera != null)
        //{
        //    virtualCameraNoise = virtualCamera.GetCinemachineComponent<Cinemachine.CinemachineBasicMultiChannelPerlin>();

        //}
    }

    // Update is called once per frame
    void Update()
    {

    }
    private void FixedUpdate()
    {
        //CheckColliders();
        MaterialCheck();
        Bubble2DestroyCheck();
        DestroyCheck();
        CheckBench2PlayerDst();
        CheckIsSitting();
    }

   void CheckIsSitting()
    {
        if((transform.position-WoodBench.transform.position).magnitude<55f)
        {
            isSitting = true;
        }
    }
    void MaterialCheck()
    {
        switch (thisState)
        {
            case (BubbleState.Dark):
            {
                    GetComponent<Renderer>().material = DarkMaterial;
                break;
            }
            case (BubbleState.Light):
            {
                    GetComponent<Renderer>().material = LightMaterial;
                    break;
            }
        }
    }
    //多人碰撞时的List数据结构
    void CheckColliders()
    {
   
            if ((colliders.transform.position - transform.position).magnitude > 0.5f)
            {
            Destroy(colliders);
            }
        
    }
    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "LightBubble")
        {
            Debug.Log("TriggerBubbleBlend");
            colliders = other.gameObject;
            if (thisState==BubbleState.Light&&other.gameObject.GetComponent<BubbleBlending>().thisState == BubbleState.Light)
            {
                IsColliding = true;
               
                //colliders.Add(other.gameObject);
                if (isBigger == false)
                {
                    BubbleCombine(gameObject, colliders);
                }
            }
         else if (thisState == BubbleState.Dark && other.gameObject.GetComponent<BubbleBlending>().thisState == BubbleState.Dark)
                {
                //CameraShake.SetDuration();
                //CameraShake.CameraShakeManager();
                AudioManager.ShakeNoise();
            }
            //    if (virtualCamera != null || virtualCameraNoise != null)
            //    {
            //        if (ShakeElapedTime > 0)
            //        {
            //            virtualCameraNoise.m_AmplitudeGain = ShakeAmplitude;
            //            virtualCameraNoise.m_FrequencyGain = ShakeFrequency;
            //            ShakeElapedTime = Time.deltaTime;
            //        }
            //        else
            //        {
            //            virtualCameraNoise.m_AmplitudeGain = 0f;
            //            ShakeElapedTime = 0f;
            //        }

            //}
            else if(thisState == BubbleState.Light && other.gameObject.GetComponent<BubbleBlending>().thisState == BubbleState.Dark)
            {
                Debug.Log("thisLightOtherDark");
                Invoke("Bubble2BubbleBlend", 3f);
                   
            }
            else if(thisState == BubbleState.Dark && other.gameObject.GetComponent<BubbleBlending>().thisState == BubbleState.Light)
            {
                Invoke("Bubble2BubbleBlend", 3f);
              
            }
        }
        else if(other.gameObject.tag=="WoodBench")
        {
                isSitting = true;
            
        }
    }

    void Bubble2BubbleBlend()
    {
        ColorChange(gameObject, 0.5f);
    }
    
    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "WoodBench")
        {
            isSitting = false;
        }
    }

    void CheckBench2PlayerDst()
    {
        //if((WoodBench.transform.position-transform.position).magnitude<2f)
        //{
        //    thisState = BubbleState.Light;
        //}
        //if((Mathf.Abs(WoodBench.transform.position.x - transform.position.x)<=2.3f)&& Mathf.Abs(WoodBench.transform.position.z - transform.position.z) <= 0.5f)
        if(isSitting)
        {
      
            if(!sitFlag)
            {
                //WoodBench.GetComponent<CheckSitNum>().Players.Add(gameObject);
                sitFlag = true;
            }
            if (WoodBench.GetComponent<CheckSitNum>().CountOfPlayers()==2)
            {
                Invoke("Bench2BubbleBlend",1.5f);

              
            }
            //thisState = BubbleState.Light;
            Debug.Log("x" + (WoodBench.transform.position.x - transform.position.x));
            Debug.Log("y" + (WoodBench.transform.position.y - transform.position.y));
            Debug.Log("z" + (WoodBench.transform.position.z - transform.position.z));
        }
        else if(colliders!=null)
        {
            // donothing
        }
        else
        {
            tempTime = 0;
        }
       
    }

    void Bench2BubbleBlend()
    {
        ColorChange(gameObject, 0.5f);
    }
    void BubbleCombine(GameObject g1,GameObject g2)
    {
        centerPos2 = (g1.transform.position + g2.transform.position) / 2f;
        bubble2Level = GameObject.Instantiate(Bubble2Level, centerPos2, g1.transform.rotation);
        bubble2Level.tag = "LightBubble2";
        bubble2Level.GetComponent<Bubble2>().G1 = g1;
        bubble2Level.GetComponent<Bubble2>().G1name = g1.name;
        bubble2Level.GetComponent<Bubble2>().G2 = g2;
        bubble2Level.transform.localScale = 2 * bubble2Level.transform.localScale;
        isBigger = true;
    }

    void Bubble2DestroyCheck()
    {
        if ( colliders != null)
        {
            if ((transform.position - colliders.transform.position).magnitude > 7.2f)
            {
                Destroy(bubble2Level);
                isBigger = false;
                //G1.SetActive(true);
                //G2.SetActive(true);
            }
        }
    }



    void ColorChange(GameObject g1,float BlendSpeed)
    {
        Debug.Log("ColorChange");
        AudioManager.BlendingAudio();

        tempTime += BlendSpeed*Time.deltaTime;
        if (tempTime>5*deltaColorTime)
        {
            g1.GetComponent<BubbleBlending>().thisState = BubbleState.Light;
        }
        else if(tempTime>4*deltaColorTime)
        {
            if(g1.GetComponent<Renderer>().material != BlendMaterial4)
            g1.GetComponent<Renderer>().material = BlendMaterial4;
        }
        else if(tempTime>3*deltaColorTime)
        {
            if (g1.GetComponent<Renderer>().material != BlendMaterial3)
                g1.GetComponent<Renderer>().material = BlendMaterial3;
        }
        else if(tempTime>2*deltaColorTime)
        {
            if (g1.GetComponent<Renderer>().material != BlendMaterial2)
                g1.GetComponent<Renderer>().material = BlendMaterial2;
        }
        else if(tempTime>deltaColorTime)
        {
            if (g1.GetComponent<Renderer>().material != BlendMaterial1)
                g1.GetComponent<Renderer>().material = BlendMaterial1;
        }
        else
        {
           
        }
    }
    void DestroyCheck()
    {
        if(Player==null)
        {
            if(bubble2Level!=null)
            {
                Destroy(bubble2Level);
            }
            Destroy(gameObject);
        }
    }
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "WoodBench")
        {
            isSitting = true;
        }
    }
    private void OnCollisionExit(Collision collision)
    {
        if (collision.gameObject.tag == "WoodBench")
        {
            isSitting = false;
        }
    }
   


}
